<?php
class friends_model_footprint extends Model {
	const PAGE_SIZE = 30;
	
	public function __construct(){
		parent::__construct();
		$this->table = 'app_footprint';
	}
	/**
	 * 保存
	 *
	 * @param int $visitor_id
	 * @param string $visitor_name
	 * @param int $host_id
	 * @param string $host_name
	 * @return int
	 */
	function save($visitor_id, $visitor_name, $host_id, $host_name){
		$params = array(
			'visitor_id'	=>	$visitor_id,
			'visitor_name'	=>	$visitor_name,
			'host_id'		=>	$host_id,
			'host_name'		=>	$host_name,
			'visit_time'	=>	time(),
			'visit_times'	=>	1
		);
		$sql = "
insert into ".$this->table." 
	(visitor_id, visitor_name, host_id, host_name, visit_time, visit_times)
values
	(&visitor_id, &visitor_name, &host_id, &host_name, &visit_time, &visit_times)
ON DUPLICATE KEY UPDATE 
	visit_time=&visit_time, visit_times=visit_times+1
		";
		$this->db->query($sql, $params);
		$id = $this->db->nextid;
//		$this->clearCache($visitor_id, $host_id, 25);
		return $id;
	}
	/**
	 * 查询来访列表页
	 *
	 * @param int $uid
	 * @param int $page
	 * @param int $pagesize
	 * @return []
	 */
	function selectIncomingByPage($uid, $page, $pagesize){
		$sql = "
select visitor_id uid, visitor_name username, visit_time from ".$this->table." where host_id=&host_id order by visit_time desc
		";
		$params['host_id'] = $uid;
		$ret = $this->db->selectPage($sql, $params, $page, $pagesize);
		
		return $ret;
	}
	/**
	 * 查询出访列表页
	 *
	 * @param int $uid
	 * @param int $page
	 * @param int $pagesize
	 * @return []
	 */
	function selectOutgoingByPage($uid, $page, $pagesize){
		$sql = "
select host_id uid, host_name username, visit_time from ".$this->table." where visitor_id=&visitor_id order by visit_time desc
		";
		$params['visitor_id'] = $uid;
		$ret = $this->db->selectPage($sql, $params, $page, $pagesize);
		
		return $ret;
	}
	/**
	 * 查询最后n条来访记录
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $n
	 * @return unknown
	 */
	function selectSidebar($uid, $n){
		$sql = "select a.visitor_id uid, a.visitor_name username, a.visit_time, b.remark from ".$this->table." a 
LEFT JOIN `app_attention` b ON (a.host_id=b.host_id AND a.visitor_id=b.uid) where a.host_id=&host_id order by a.visit_time desc limit $n";
		$params = array('host_id'=>$uid);
		$rows = $this->db->fetchAll($sql, $params);
		return $rows;
	}
	
	/**
	 * 清除cache
	 *
	 * @param unknown_type $vid
	 * @param unknown_type $hid
	 * @param unknown_type $pagesize
	 */
	function clearCache($vid, $hid, $pagesize){
		
		Cache::cleanFunctionCache(array($this, 'selectIncomingByPage'), array($hid, 1, $pagesize));
		Cache::cleanFunctionCache(array($this, 'selectOutgoingByPage'), array($vid, 1, $pagesize));
		Cache::cleanFunctionCache(array($this, 'selectSidebar'), array($hid, 10));
	}
	
	
	
	
	
	
	
	
	
	/*
	public function getRow($ids)
	{
		$ids = (array) $ids;
		$ids = implode(',', $ids);
		return $this->db->fetchAll("SELECT * FROM " . $this->table . " WHERE id IN({$ids})");
	}
	*/
	
	/**
	 * 获取用户详细信息
	 *
	 * @param array $uids
	 * @return array
	 */
	public function getRow($uids)
	{
		$uids = implode(',', (array) $uids);
		
		return $this->db->fetchAll("SELECT * FROM prop_standard WHERE id IN({$uids})");
	}
	
	public function getInListId2($hostId)
	{
		$listId = $this->db->fetchAll("select visitor_id,visit_time from " . $this->table . " where host_id=&host_id order by visit_time desc", array('host_id' => $hostId));
		
		$uids = $times = array();
		foreach ($listId as $v) {
			$uids[] = $v['visitor_id'];
			$times[$v['visitor_id']] = $v['visit_time'];
			//$result[] = $v['id'];
		}
		//return $result;
		return array($uids, $times);
	}
	
	public function getOutListId2($visitorId)
	{
		$listId = $this->db->fetchAll("select host_id,visit_time from ".$this->table." where visitor_id=&visitor_id order by visit_time desc", array('visitor_id' => $visitorId));
		
		//$result = array();
		$uids = $times = array();
		foreach ($listId AS $v) {
			$uids[] = $v['host_id'];
			$times[$v['host_id']] = $v['visit_time'];
			//$result[] = $v['id'];
		}
		//return $result;
		return array($uids, $times);
	}
}